﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class AdminPromo : BaseAdminModule
    {
        [AccessedThroughProperty("cmdDelete")]
        private LinkButton _cmdDelete;
        [AccessedThroughProperty("cmdNew")]
        private LinkButton _cmdNew;
        [AccessedThroughProperty("cmdSearch")]
        private LinkButton _cmdSearch;
        private string _CodeType;
        [AccessedThroughProperty("chkAllowMultiDiscount")]
        private CheckBox _chkAllowMultiDiscount;
        [AccessedThroughProperty("gvPromo")]
        private GridView _gvPromo;
        [AccessedThroughProperty("plAllowMultiDiscount")]
        private UserControl _plAllowMultiDiscount;
        [AccessedThroughProperty("pnlEdit")]
        private Panel _pnlEdit;
        [AccessedThroughProperty("pnlProduct")]
        private Panel _pnlProduct;
        [AccessedThroughProperty("pnlRange")]
        private Panel _pnlRange;
        [AccessedThroughProperty("txtSearch")]
        private TextBox _txtSearch;

        public AdminPromo()
        {
            base.Load += new EventHandler(this.Page_Load);
        }

        private void AddNewRecord(int ObjectID, string Description)
        {
            PromoController controller = new PromoController();
            NB_Store_PromoInfo objInfo = new NB_Store_PromoInfo {
                PromoID = 0,
                ObjectID = ObjectID,
                Range1 = Conversions.ToString(0),
                Range2 = Conversions.ToString(0),
                PromoName = DotNetNuke.Services.Localization.Localization.GetString("New_Promotion", this.LocalResourceFile),
                PromoType = this._CodeType.ToUpper(),
                Disabled = false,
                PromoAmount = Conversions.ToString(0),
                PromoGroup = "",
                PromoPercent = 0,
                PortalID = this.PortalId,
                RangeEndDate = Conversions.ToDate(DateAndTime.DateAdd(DateInterval.Year, 10.0, DateAndTime.Today).ToString()),
                RangeStartDate = Conversions.ToDate(DateAndTime.Today.ToString()),
                PromoCode = "",
                QtyRange1 = 0,
                QtyRange2 = 0,
                MaxUsagePerUser = 0
            };
            controller.UpdateObjPromo(objInfo);
            SharedFunctions.UpdateLog(this.UserId, this.ControlName + " - " + MethodBase.GetCurrentMethod().Name + " - " + XmlUtils.Serialize(objInfo));
        }

        private void cmdNew_Click(object sender, EventArgs e)
        {
            this.AddNewRecord(-1, "");
            this.Response.Redirect(this.Request.Url.ToString());
        }

        private void cmdSearch_Click(object sender, EventArgs e)
        {
            this.populateGV();
        }

        private void chkAllowMultiDiscount_CheckedChanged(object sender, EventArgs e)
        {
            SharedFunctions.SetStoreSetting(this.PortalId, "allowmultidiscount.flag", this.chkAllowMultiDiscount.Checked.ToString(), "None", false);
        }

        private void deleteGVrow(GridViewDeleteEventArgs e)
        {
            PromoController controller = new PromoController();
            NB_Store_PromoInfo info = new NB_Store_PromoInfo();
            GridViewRow row = this.gvPromo.Rows[e.RowIndex];
            controller.DeletePromo(SharedFunctions.getGridViewValueInt(row, "PromoID"));
            SharedFunctions.UpdateLog(this.UserId, this.ControlName + " - " + MethodBase.GetCurrentMethod().Name + " - " + XmlUtils.Serialize(info));
        }

        private void gvDiscount_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewRow row = e.Row;
            if (row.RowType == DataControlRowType.DataRow)
            {
                LinkButton button = (LinkButton) e.Row.FindControl("cmdDelete");
                button.Text = "<img src=\"" + base.StoreInstallPath + "img/delete.png\" border=\"0\" />";
                button.Attributes.Add("onClick", "javascript:return confirm('" + DotNetNuke.Services.Localization.Localization.GetString("cmdDelete", this.LocalResourceFile) + "');");
                if (row.RowIndex == this.gvPromo.EditIndex)
                {
                    TextBox box;
                    HyperLink link = (HyperLink) e.Row.FindControl("hypDateRange1");
                    if (link != null)
                    {
                        box = (TextBox) e.Row.FindControl("txtDateRange1");
                        if (box != null)
                        {
                            link.Text = "<img src=\"" + DotNetNuke.Common.Globals.ApplicationPath + "/images/calendar.png\" border=\"0\" />";
                            link.NavigateUrl = DotNetNuke.Common.Utilities.Calendar.InvokePopupCal(box);
                        }
                    }
                    link = (HyperLink) e.Row.FindControl("hypDateRange2");
                    if (link != null)
                    {
                        box = (TextBox) e.Row.FindControl("txtDateRange2");
                        if (box != null)
                        {
                            link.Text = "<img src=\"" + DotNetNuke.Common.Globals.ApplicationPath + "/images/calendar.png\" border=\"0\" />";
                            link.NavigateUrl = DotNetNuke.Common.Utilities.Calendar.InvokePopupCal(box);
                        }
                    }
                    DropDownList ddlCategories = (DropDownList) e.Row.FindControl("ddlCategories");
                    if (ddlCategories != null)
                    {
                        SharedFunctions.populateCategoryList(this.PortalId, ddlCategories, Conversions.ToString(-1), "All Categories", ((NB_Store_PromoInfo) row.DataItem).ObjectID.ToString());
                    }
                    DropDownList ddl = (DropDownList) e.Row.FindControl("ddlPromoGroup");
                    if (ddl != null)
                    {
                        this.populateRoleList(ddl, ((NB_Store_PromoInfo) row.DataItem).PromoGroup);
                    }
                }
            }
        }

        private void gvDiscount_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.gvPromo.EditIndex = e.NewEditIndex;
            this.populateGV();
            this.setupFields();
        }

        private void gvDiscount_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (this.Page.IsValid)
            {
                GridViewRow row = this.gvPromo.Rows[e.RowIndex];
                DateTime time2 = SharedFunctions.getGridViewValueDate(row, "txtDateRange1");
                DateTime time = SharedFunctions.getGridViewValueDate(row, "txtDateRange2");
                this.updateGVrow(e);
                this.gvPromo.EditIndex = -1;
                this.populateGV();
                if ((this._CodeType.ToUpper() == "STO") && ((DateTime.Compare(time2, DateAndTime.Today) <= 0) & (DateTime.Compare(time, DateAndTime.Today) >= 0)))
                {
                    new PromoController().createSalePriceTable(this.PortalId, -1);
                }
            }
        }

        private void gvPromo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.gvPromo.EditIndex = -1;
            this.populateGV();
        }

        private void gvPromo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = this.gvPromo.Rows[e.RowIndex];
            DateTime time2 = SharedFunctions.getGridViewValueDate(row, "txtDateRange1");
            DateTime time = SharedFunctions.getGridViewValueDate(row, "txtDateRange2");
            this.deleteGVrow(e);
            this.gvPromo.EditIndex = -1;
            this.populateGV();
            if ((this._CodeType.ToUpper() == "STO") && ((DateTime.Compare(time2, DateAndTime.Today) <= 0) & (DateTime.Compare(time, DateAndTime.Today) >= 0)))
            {
                new PromoController().createSalePriceTable(this.PortalId, -1);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (this.Request.QueryString["spg"] != null)
                {
                    switch (this.Request.QueryString["spg"].ToUpper())
                    {
                        case "COU":
                            this._CodeType = this.Request.QueryString["spg"].ToUpper();
                            break;

                        case "CAR":
                            this._CodeType = this.Request.QueryString["spg"].ToUpper();
                            break;

                        case "STO":
                            this._CodeType = this.Request.QueryString["spg"].ToUpper();
                            break;
                    }
                    if (!this.Page.IsPostBack)
                    {
                        this.cmdDelete.Visible = false;
                        string str3 = this._CodeType.ToUpper();
                        if (str3 == "COU")
                        {
                            this.pnlProduct.Visible = true;
                        }
                        else if (str3 == "CAR")
                        {
                            this.pnlProduct.Visible = false;
                        }
                        else if (str3 == "STO")
                        {
                            this.pnlProduct.Visible = false;
                        }
                        else
                        {
                            this.pnlEdit.Visible = false;
                        }
                        string str = SharedFunctions.GetStoreSetting(this.PortalId, "allowmultidiscount.flag", "None");
                        if (str == "")
                        {
                            str = "false";
                        }
                        this.chkAllowMultiDiscount.Checked = Conversions.ToBoolean(str);
                        this.populateGV();
                    }
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException((PortalModuleBase) this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void populateGV()
        {
            PromoController controller = new PromoController();
            GridView gvPromo = this.gvPromo;
            DotNetNuke.Services.Localization.Localization.LocalizeGridView(ref gvPromo, this.LocalResourceFile);
            this.gvPromo = gvPromo;
            ArrayList list = controller.GetPromoList(this.PortalId, this._CodeType.ToUpper(), this.txtSearch.Text, false);
            this.gvPromo.DataSource = list;
            this.gvPromo.DataBind();
        }

        private void populateRoleList(DropDownList ddl, string SelectedRole)
        {
            SettingsController controller = new SettingsController();
            ddl.Items.Clear();
            NB_Store_SettingsInfo info = controller.GetSetting(this.PortalId, "promo.roles", "None");
            if (info != null)
            {
                string[] strArray = ("," + info.SettingValue).Split(new char[] { ',' });
                int upperBound = strArray.GetUpperBound(0);
                for (int i = 0; i <= upperBound; i++)
                {
                    ddl.Items.Add(strArray[i]);
                }
                if (ddl.Items.Count == 1)
                {
                    ddl.Enabled = false;
                }
            }
            else
            {
                ddl.Enabled = false;
            }
            if (ddl.Items.FindByValue(SelectedRole) != null)
            {
                ddl.ClearSelection();
                ddl.Items.FindByValue(SelectedRole).Selected = true;
            }
        }

        private void setupFields()
        {
            GridViewRow row = this.gvPromo.Rows[this.gvPromo.EditIndex];
            string ctrlIdAsCSVlist = "";
            string str2 = this._CodeType.ToUpper();
            if (str2 != "COU")
            {
                if (str2 == "CAR")
                {
                    ctrlIdAsCSVlist = "plPromoCode,";
                    ctrlIdAsCSVlist = (((ctrlIdAsCSVlist + "plCategories,") + "txtPromoCode," + "ddlCategories,") + "plMaxUsagePerUser," + "txtMaxUsagePerUser,") + "plMaxUsage," + "txtMaxUsage";
                }
                else if (str2 == "STO")
                {
                    ctrlIdAsCSVlist = "plPromoCode,";
                    ctrlIdAsCSVlist = ((((((((((ctrlIdAsCSVlist + "lblQtySep,") + "lblPriceSep," + "plPromoUser,") + "plPromoCode," + "plPromoUser,") + "plQtyRange," + "plRange,") + "plPromoEmail," + "txtPromoCode,") + "txtPromoUser," + "txtPromoCode,") + "txtPromoUser," + "txtQtyRange1,") + "txtQtyRange2," + "txtRange1,") + "txtRange2," + "txtPromoEmail,") + "plMaxUsagePerUser," + "txtMaxUsagePerUser,") + "plMaxUsage," + "txtMaxUsage";
                }
                else
                {
                    this.pnlEdit.Visible = false;
                }
            }
            if (ctrlIdAsCSVlist != "")
            {
                SharedFunctions.setGridViewVisible(row, ctrlIdAsCSVlist, false);
            }
        }

        private void updateGVrow(GridViewUpdateEventArgs e)
        {
            PromoController controller = new PromoController();
            NB_Store_PromoInfo objInfo = new NB_Store_PromoInfo();
            GridViewRow row = this.gvPromo.Rows[e.RowIndex];
            objInfo.Disabled = !SharedFunctions.getGridViewValueBool(row, "chkDisable");
            objInfo.PromoAmount = Conversions.ToString(SharedFunctions.getGridViewValueDbl(row, "txtAmount"));
            objInfo.PromoCode = SharedFunctions.getGridViewValue(row, "txtPromoCode");
            objInfo.PromoGroup = SharedFunctions.getGridViewValue(row, "ddlPromoGroup");
            objInfo.PromoUser = SharedFunctions.getGridViewValue(row, "txtPromoUser");
            objInfo.PromoID = SharedFunctions.getGridViewValueInt(row, "PromoID");
            objInfo.PromoName = SharedFunctions.getGridViewValue(row, "txtName");
            objInfo.PromoPercent = (int) Math.Round(SharedFunctions.getGridViewValueDbl(row, "txtPercent"));
            objInfo.PromoType = SharedFunctions.getGridViewValue(row, "PromoType");
            objInfo.ObjectID = SharedFunctions.getGridViewValueInt(row, "ObjectID");
            objInfo.PortalID = this.PortalId;
            objInfo.Range1 = Conversions.ToString(SharedFunctions.getGridViewValueDbl(row, "txtRange1"));
            objInfo.Range2 = Conversions.ToString(SharedFunctions.getGridViewValueDbl(row, "txtRange2"));
            objInfo.RangeEndDate = SharedFunctions.getGridViewValueDate(row, "txtDateRange2");
            objInfo.RangeStartDate = SharedFunctions.getGridViewValueDate(row, "txtDateRange1");
            objInfo.QtyRange1 = SharedFunctions.getGridViewValueInt(row, "txtQtyRange1");
            objInfo.QtyRange2 = SharedFunctions.getGridViewValueInt(row, "txtQtyRange2");
            objInfo.PromoEmail = SharedFunctions.getGridViewValue(row, "txtPromoEmail");
            objInfo.ObjectID = SharedFunctions.getGridViewValueInt(row, "ddlCategories");
            objInfo.MaxUsagePerUser = SharedFunctions.getGridViewValueInt(row, "txtMaxUsagePerUser");
            objInfo.MaxUsage = SharedFunctions.getGridViewValueInt(row, "txtMaxUsage");
            controller.UpdateObjPromo(objInfo);
            SharedFunctions.UpdateLog(this.UserId, this.ControlName + " - " + MethodBase.GetCurrentMethod().Name + " - " + XmlUtils.Serialize(objInfo));
        }

        protected virtual LinkButton cmdDelete
        {
            get
            {
                return this._cmdDelete;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._cmdDelete = value;
            }
        }

        protected virtual LinkButton cmdNew
        {
            get
            {
                return this._cmdNew;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdNew_Click);
                if (this._cmdNew != null)
                {
                    this._cmdNew.Click -= handler;
                }
                this._cmdNew = value;
                if (this._cmdNew != null)
                {
                    this._cmdNew.Click += handler;
                }
            }
        }

        protected virtual LinkButton cmdSearch
        {
            get
            {
                return this._cmdSearch;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdSearch_Click);
                if (this._cmdSearch != null)
                {
                    this._cmdSearch.Click -= handler;
                }
                this._cmdSearch = value;
                if (this._cmdSearch != null)
                {
                    this._cmdSearch.Click += handler;
                }
            }
        }

        protected virtual CheckBox chkAllowMultiDiscount
        {
            get
            {
                return this._chkAllowMultiDiscount;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.chkAllowMultiDiscount_CheckedChanged);
                if (this._chkAllowMultiDiscount != null)
                {
                    this._chkAllowMultiDiscount.CheckedChanged -= handler;
                }
                this._chkAllowMultiDiscount = value;
                if (this._chkAllowMultiDiscount != null)
                {
                    this._chkAllowMultiDiscount.CheckedChanged += handler;
                }
            }
        }

        protected virtual GridView gvPromo
        {
            get
            {
                return this._gvPromo;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                GridViewEditEventHandler handler = new GridViewEditEventHandler(this.gvDiscount_RowEditing);
                GridViewDeleteEventHandler handler2 = new GridViewDeleteEventHandler(this.gvPromo_RowDeleting);
                GridViewCancelEditEventHandler handler3 = new GridViewCancelEditEventHandler(this.gvPromo_RowCancelingEdit);
                GridViewUpdateEventHandler handler4 = new GridViewUpdateEventHandler(this.gvDiscount_RowUpdating);
                GridViewRowEventHandler handler5 = new GridViewRowEventHandler(this.gvDiscount_RowDataBound);
                if (this._gvPromo != null)
                {
                    this._gvPromo.RowEditing -= handler;
                    this._gvPromo.RowDeleting -= handler2;
                    this._gvPromo.RowCancelingEdit -= handler3;
                    this._gvPromo.RowUpdating -= handler4;
                    this._gvPromo.RowDataBound -= handler5;
                }
                this._gvPromo = value;
                if (this._gvPromo != null)
                {
                    this._gvPromo.RowEditing += handler;
                    this._gvPromo.RowDeleting += handler2;
                    this._gvPromo.RowCancelingEdit += handler3;
                    this._gvPromo.RowUpdating += handler4;
                    this._gvPromo.RowDataBound += handler5;
                }
            }
        }

        protected virtual UserControl plAllowMultiDiscount
        {
            get
            {
                return this._plAllowMultiDiscount;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plAllowMultiDiscount = value;
            }
        }

        protected virtual Panel pnlEdit
        {
            get
            {
                return this._pnlEdit;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._pnlEdit = value;
            }
        }

        protected virtual Panel pnlProduct
        {
            get
            {
                return this._pnlProduct;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._pnlProduct = value;
            }
        }

        protected virtual Panel pnlRange
        {
            get
            {
                return this._pnlRange;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._pnlRange = value;
            }
        }

        protected virtual TextBox txtSearch
        {
            get
            {
                return this._txtSearch;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._txtSearch = value;
            }
        }
    }
}

